package com.example.admin.goodnews.fragment.XinWenFenLei;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.activity.SecondaryPageActivity;
import com.example.admin.goodnews.adapter.XRecyclerViewAdapter;
import com.example.admin.goodnews.entity.GoodNewsDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 财经最新页面
 * Created by admin on 2016/10/10.
 */

public class CaiJingZuiXinFragment extends Fragment implements RecyclerViewItemClickListener,RecyclerViewItemLongClickListener {
    private XRecyclerView xrecyclerview;// 用于加载数据的列表
    private XRecyclerViewAdapter xRecyclerViewAdapter;// 国内焦点页面的适配器
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    private RelativeLayout relativelayout;// 用来显示 Snackbar
    private List<GoodNewsDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> datalists = new ArrayList<>();// 适配器需要的数据列表
    private List<GoodNewsDataEntity.ShowapiResBodyBean.PagebeanBean.ContentlistBean> lists = new ArrayList<>();// 存放临时数据的列表
    private String channelName = "财经最新";
    private int page = 1;// 页数，用于上拉加载和下拉刷新
    private String time = GetCurrentTimeUtil.getCurrentTime();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_tongyongbuju, null);
        xrecyclerview = (XRecyclerView) view.findViewById(R.id.xrecyclerview_tongyongbuju);// 用于加载数据的列表
        relativelayout = (RelativeLayout) view.findViewById(R.id.relativelayout_tongyongbuju);// 用来显示 Snackbar
        // 设置布局管理器
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        xrecyclerview.setLayoutManager(linearLayoutManager);
        // 下拉刷新和上拉加载
        xrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
        getData();
        return view;
    }

    private void getData() {
        String param = NetWorkDataUrl.TONGYONG_PARAM1 + channelName + NetWorkDataUrl.TONGYONG_PARAM2 + page + NetWorkDataUrl.TONGYONG_PARAM3 + time + NetWorkDataUrl.TONGYONG_PARAM4;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.TONGYONG_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    lists = gson.fromJson(result, GoodNewsDataEntity.class).getShowapi_res_body().getPagebean().getContentlist();
                    if (lists.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Snackbar.make(relativelayout, "服务器君离家出走了~", Snackbar.LENGTH_SHORT).show();
                    break;
                case 1:
                    if (page == 1) {
                        datalists.clear();
                        datalists.addAll(lists);
                        xRecyclerViewAdapter = new XRecyclerViewAdapter(getActivity(), datalists);// 初始化适配器
                        xrecyclerview.setAdapter(xRecyclerViewAdapter);// 给RecyclerView设置适配器
                        xrecyclerview.refreshComplete();
                        lists.clear();
                    } else {
                        datalists.addAll(lists);
                        xRecyclerViewAdapter.notifyDataSetChanged();
                        xrecyclerview.loadMoreComplete();
                        lists.clear();
                    }
                    xRecyclerViewAdapter.setRecyclerViewItemClickListener(CaiJingZuiXinFragment.this);
                    xRecyclerViewAdapter.setRecyclerViewItemLongClickListener(CaiJingZuiXinFragment.this);
                    break;
            }
        }
    };

    @Override
    public void RecyclerViewItemClickListener(int position) {
        String link = datalists.get(position).getLink();
        String title = datalists.get(position).getChannelName();
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("link", link);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    @Override
    public void RecyclerViewItemLongClickListener(int position) {
        String link = datalists.get(position).getLink();
        String title = datalists.get(position).getChannelName();
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("link", link);
        intent.putExtra("title", title);
        startActivity(intent);
    }
}
